﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;
using BLL;
using DTO;

namespace CRM.GUI.QLHeThong
{
    public partial class ucCollector : UserControl
    {
        Administrators ad = new Administrators();
        BLsupervisor sup = new BLsupervisor();
        cDataInTransfer data = new cDataInTransfer();
        private List<CollectorEntity> _list;

        //khai bao colector Select
        CollectorEntity colSelected = null;

        public ucCollector()
        {
            InitializeComponent();
            _list = data.LoadListCollector();
            colRegion.DataSource = SharedData.RegionList;
            colRegion.DisplayMember = "RegionName";
            colRegion.ValueMember = "Region_Code";
            colCollector.DataSource = _list;
            colCollector.DisplayMember = "Collector_Code";
            colCollector.ValueMember = "Collector_ID";
            grdCollector.DataSource = _list;

            cmbRegion.DataSource = SharedData.RegionList.Where(a => a.RegionLevel == 2).ToList();
            cmbRegion.ValueMember = "Region_Code";
            cmbRegion.DisplayMember = "RegionName";


            cmbRegion_Up.DataSource = SharedData.RegionList.Where(a => a.RegionLevel == 2).ToList();
            cmbRegion_Up.ValueMember = "Region_Code";
            cmbRegion_Up.DisplayMember = "RegionName";

        }

        private void btnAddNewCollector_Click(object sender, EventArgs e)
        {
            if (txtCollector_ID.Text == "" || txtFullName.Text == "")
            {
                MessageBox.Show("Không được bỏ trống mã nhân viên và tên", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                ad.AddNewCollector(txtCollector_ID.Text, txtFullName.Text, (int)cmbRegion.SelectedValue);

                //refesh
                grdCollector.DataSource = ad.LoadListCollector();
                txtCollector_ID.Text = "";
                txtFullName.Text = "";
                cmbRegion.SelectedItem = null;
                
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (colSelected != null)
            {
                colSelected.FullName = txtFullName_Up.Text;
                colSelected.Region_ID = (int)cmbRegion_Up.SelectedValue;
                ad.UpdateCollector(colSelected);
                grdCollector.DataSource = ad.LoadListCollector();

                //refesh
                txtCollector_ID_Up.Text  = "";
                txtFullName_Up.Text = "";
                cmbRegion_Up.SelectedItem = null;
            }
        }

        private void grdViewCollector_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            colSelected = ((CollectorEntity)grdViewCollector.GetRow(e.RowHandle));
            txtCollector_ID_Up.Text = colSelected.Collector_Code;
            txtFullName_Up.Text = colSelected.FullName;
            cmbRegion_Up.SelectedValue = colSelected.Region_ID;
        }

    }
}
